<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\BaseAdminController;
use App\Lib\Enum\ValRule;
use App\Lib\Logger;
use App\Lib\Validates;
use App\Rules\CheckRule;
use App\Rules\RuleFun;
use App\Service\AdminUserService;
use App\Service\LoginService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;

class LoginController extends BaseAdminController {
    //登录模板
    public function loginShow() {
        return view('admin.admin_user.login');
    }

    //登录
    public function login(Request $request) {
        //验证参数
        $params = RuleFun::params($request, ValRule::LOGIN, ValRule::LOGIN_MSG);
        if (is_array($params) && array_key_exists("code", $params)) {
            return redirect(\route("admin.login"))->withErrors($params['data']);
        }
        //验证登陆信息
        $notice=(new LoginService())->loginUser($params,$request);
        if($notice["error"]>0){
            return $this->tempNotice($notice['url'],$notice['msg'],"error");
        }
        //首次登录修改密码
        if($this->getCurrentUser()["pwd_init"]==1){
            return redirect(route("admin.changeShow"));
        }
        (new AdminUserService())->setLogin($request,1,$params["name"],$params["guard_name"]);
        //记录日志
        $this->log(Logger::LOG_LEVEL_INFO,'login','login',"用户".getCurrentUser()["name"]."登录成功",[]);
        //登陆后跳转
        if(!session("url.intended")||Str::contains(session("url.intended"), 'login')){
            return $params['guard_name']=="system"?redirect(route('system.index')):redirect(route('admin.index'));
        }
        //跳转未登录之前的页面
        return $params['guard_name']=="system"?redirect()->intended(\route('system.index')):redirect()->intended(\route('admin.index'));
    }

    //退出
    public function logout(){
        $msg = empty(getCurrentUser())?"未登录用户操作":"用户".getCurrentUser()["name"]."退出";
        $this->log(Logger::LOG_LEVEL_INFO,'logout','logout',$msg,[]);
        auth(session('guard'))->logout();
        session()->forget(['guard']);
        return redirect(\route("admin.login"));
    }

    //首次登录修改密码
    public function changeShow(){
        return view('admin.admin_user.resetpwd');
    }

    public function change($id,Request $request){
        $check = Validates::isPositiveInteger($id);
        if (!$check) {
            return $this->tempNotice("admin.changeShow","id必须是正整数","error");
        }
        $params = (new CheckRule($request->all(),ValRule::PWD_STORE,ValRule::PWD_STORE_MSG))->check();
        if($params["error"]>0){
            return $this->tempNotice("admin.changeShow",$params["msg"],"error");
        }
        $result = (new AdminUserService())->changePwd($id,$params["data"]['password']);
        return $result?(session('guard')=="system"?redirect(route("system.index")):redirect(route("admin.index"))):$this->tempNotice("admin.changeShow","修改错误","error");
    }

    //已经登陆提示跳转首页
    public function already(){
        return view("errors.alreadyLogin");
    }
}
